//
//  UTString+SubString.swift
//  iPhoneTests
//
//  Created by Toj on 3/28/23.
//

import XCTest

class UTString_SubString: XCTestCase {

    override func setUpWithError() throws {
        print("-->UTests: rtyuio ")
    }

    override func tearDownWithError() throws {
        print("-->UTests: rtyuio ")
    }
    
    func testExample() throws {
        print("-->UTests: start \n")
        
        setupMakeAccount()
       
        test_substring()
        test_cs_substring()
        test_ns_substring()
        test_swift_substring()
        
        print("-->UTests: end \n")
    }
    
    func testPerformanceExample() throws {
        print("-->UTests: rtyuio ")
        self.measure {
            print("-->UTests: rtyuio ")
        }
    }
    // "1234567890 qwer5yuio0 asdf5ghjkl"
    let signatureKey = "1234567890qwer5yuio0asdf5ghjkl"
    var from = 0
    var to = 0
    var range = NSRange(location: 0, length: 0)
}

// UT Case
extension UTString_SubString {
    /*
    email@123.com
    E123456
    email:
    1位: E@123.com
    **@123.com

    [2, 3]位: E1@123.com / E12@123.com
    E**@123.com / E1**@123.com

    [4,5]位: E123@123.com / E1234@123.com
    E12**@123.com

    (5,~]位: E12345@123.com
    E12**45@123.com

    // 正解
    -->UTests: prefix            email:E@123.com, text:       **@123.com
    -->UTests: prefix           email:E1@123.com, text:      E**@123.com
    -->UTests: prefix          email:E12@123.com, text:     E1**@123.com
    -->UTests: prefix         email:E123@123.com, text:    E12**@123.com
    -->UTests: prefix        email:E1234@123.com, text:    E12**@123.com

    -->UTests: prefix       email:E12345@123.com, text:  E12**45@123.com
    -->UTests: prefix      email:E123456@123.com, text:  E12**56@123.com
    -->UTests: prefix     email:E1234567@123.com, text:  E12**67@123.com
    -->UTests: prefix    email:E12345678@123.com, text:  E12**78@123.com
    -->UTests: prefix   email:E123456789@123.com, text:  E12**89@123.com
    -->UTests: prefix email:E12345678910@123.com, text:  E12**10@123.com
     */
    func setupMakeAccount() {
        let emailSuffix = "@123.com"
        var account = ""
        var email = account + emailSuffix
        let accountText = test_replaceEmail(email)
        print("-->toj: account:\(email), text: \(accountText)")
        
        account = "E"
        
        for i in 0...10 {
            if i > 0 { account += "\(i)" }
            email = account + emailSuffix
            let accountText = test_replaceEmail(email)
            print("-->toj: account:\(email), text: \(accountText)")
            
        }
    }
    
    func test_other() {
//        var key = ""
//        var length = 0
//        var signature = "1234567890qwer5yuio0asdf5ghjkl"
//        // "1234567890qwer5yuio0asdf5ghjkl"
//        length = 15 // yuio0asdf5ghjkl
//        key = signature.suffix(length).to_string
//        // print("-->UTests: swift suffix key:\(key)")
//
//        var account = "E1234"
//        let emailSuffix = "@123.com"
//        for i in 5...10 {
//            if i > 0 { account += "\(i)" }
//            let email = account + emailSuffix
//            let text = test_replaceEmail(email)
//            print("-->UTests: prefix email:\(email), text:\(text)")
//
//        }
    }
  
    func test_replaceEmail(_ email: String) -> String {
        var emailText = email
        
        let components = email.components(separatedBy: "@")
        let domain = components.last ?? ""
        let emailSuffix = "@" + domain
        var uid = components.first ?? ""
        
        guard !uid.isEmpty else { return emailText }
        
        let replaceString = "**"
        let uidLength = uid.count
        if uidLength < 6 { // [1,5]
            let suffixCount = uidLength < 5 ? 1 : 2
            let length = uidLength - suffixCount
            emailText = String(uid.prefix(length)) + replaceString + emailSuffix
            // print("-->UTests: uidString prefix: \(emailText) ")
            return emailText
        }
        
        // [6,~]
        let startRange = uid.index(uid.startIndex, offsetBy: 3)
        let endRange = uid.index(uid.startIndex, offsetBy: uid.count-2)
        let range = startRange..<endRange
        uid.replaceSubrange(range, with: replaceString)
        emailText = uid + emailSuffix
        // print("-->UTests: uidString range: \(emailText) ")
        return emailText
    }
    
    /**
     * substring
     */
    func test_prefix(_ email: String) -> String {
        var emailText = email
        
        let components = email.components(separatedBy: "@")
        let domain = components.last ?? ""
        let emailSuffix = "@" + domain
        let uid = components.first ?? ""
        guard !uid.isEmpty else { return emailText }
        
        let replaceString = "**"
        let uidLength = uid.count - 1
        let uidString = uid.prefix(uidLength).to_string
        emailText = uidString + replaceString + emailSuffix
        
        return emailText
    }
   
    /**
     * substring
     */
    func test_substring() {
        let count = signatureKey.count
//        from = count-1
//        to = count+1
         range = NSRange(location: count-1, length: 3)
//        range = NSRange(location: 20, length: 4)
        print("-->UTests: string: \(signatureKey), count: \(count), ")
        
        test_cs_substring()
        
//        to = count
        range = NSRange(location: 0, length: 1)
        print("-->UTests: ")
        test_ns_substring()
        
        //print("-->UTests: ")
        //test_swift_substring()
        
        test_substring2()
    }
    
    func test_substring2() {
        let prefixString = "startimes://platformapi/webtoapp?target=c3RhcnRpbWVzOi8vcGxheWVyP3Byb2dyYW1EZXRhaWxJZD0xMDEyNyZ2b2RJZD0yMTU5OTQ="

        var keyString   = ""
        var valueString = ""
        
        keyString   = prefixString.st_subsring(to: "=")
        print("-->UTests: substring to substring:\(keyString)")
        valueString = prefixString.st_subsring(to: "=")
        print("-->UTests: substring to substring:\(valueString)")
        
        keyString   = prefixString.st_subsring(from: "=")
        print("-->UTests: substring from substring:\(keyString)")
        valueString = prefixString.st_subsring(from: "=")
        print("-->UTests: substring from substring:\(valueString)")
     }
    
    func test_cs_substring() {
        var substring = ""
//        substring = signatureKey.cca_subsring(from: from)
//        print("-->UTests: cs from substring:\(substring)")
        
//        substring = signatureKey.cca_subsring(to: to)
//        print("-->UTests: cs to substring:\(substring)")
        
        substring = signatureKey.cca_subsring(range: range)
        print("-->UTests: cs from substring:\(substring)")
        
        substring = signatureKey.cca_subsring(from: range.location, length: range.length)
        print("-->UTests: cs from substring:\(substring)")
    }
    
    func test_ns_substring() {
        var substring = ""
//        var substring = NSString(string: signatureKey).substring(from: from)
//        print("-->UTests: ns from substring:\(substring)")
        
//        substring = NSString(string: signatureKey).substring(to: to)
//        print("-->UTests: ns to substring:\(substring)")
        
        substring = NSString(string: signatureKey).substring(with: range)
        print("-->UTests: ns NSRange substring:\(substring)")
    }

    func test_swift_substring() {
        
        var length = 10 // 0asdf5hjkl0
        let index = String.Index(utf16Offset: length, in: signatureKey)
        var key = signatureKey[..<index]
        print("-->UTests: swift ..< key:\(key)")
        
        length = 15 // 1234567890qwer5
        key = signatureKey.prefix(length)
        print("-->UTests: swift prefix key:\(key)")
        let sss = "1234"
        if key == sss { }
        length = 15 // yuio0asdf5ghjkl
        key = signatureKey.suffix(length)
        print("-->UTests: swift suffix key:\(key)")
    }
}
